const app = getApp();
const util = require('../../utils/util.js');
const {
  $Toast
} = require('../../dist/base/index');

Page({
  data: {
		friendphone: '',
    isShowModal: true
  },

  onLoad: function(options) {
    var userInfo = wx.getStorageSync("loginInfo");

    if (!userInfo) {
			$Toast({
				content: '请先登录!',
				type: 'error'
			});
      wx.reLaunch({
        url: '/pages/userinfo/userinfo',
      });
    } else {
      this.setData({
        userInfo: userInfo
      });

      this.getFriendList();
    }
  },

	toChat: function(e) {
		var that = this;
		var index = e.currentTarget.dataset.current;

		var chatInfo = {
			userInfo: {
				account: that.data.userInfo.account,
				app_sid: that.data.userInfo.app_sid,
				avatar: that.data.userInfo.avatar,
				username: that.data.userInfo.username
			},
			friendInfo: {
				account: that.data.friendList[index].friendphone,
				app_sid: that.data.userInfo.app_sid,
				avatar: that.data.friendList[index].avatar,
				username: that.data.friendList[index].username
			}
		}

		wx.setStorage({
			key: 'chatInfo',
			data: chatInfo,
		});

		//跳转到聊天界面
		wx.navigateTo({
			url: '/pages/chat/chat',
		})
	},

  onReady: function() {

  },

  onShow: function() {

  },

  onHide: function() {

  },

  onUnload: function() {

  },

  onPullDownRefresh: function() {

  },

  onReachBottom: function() {

  },

  onShareAppMessage: function() {

  },

	group: function() {
		$Toast({
			content: '尚待开发!',
			type: 'error'
		});
	},

	goods: function () {
		$Toast({
			content: '尚待开发!',
			type: 'error'
		});
	},

  cancel: function () {
    this.setData({
      isShowModal: !this.data.isShowModal
    });
  },
  commentInput: function (e) {
    this.setData({
      rename: e.detail.value
    });
  },
  showModal: function () {
    this.setData({
      isShowModal: false
    });
  },
  confirm: function () {
    var that = this;

    wx.request({
      url: util.basePath + '/article/v1/renameFriend',
      method: "post",
      data: {
        id: that.data.friend_id,
        rename: that.data.rename
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data.status == 200) {
          that.setData({
            isShowModal: !that.data.isShowModal,
            rename: ''
          });
          $Toast({
            content: res.data.payload,
            type: 'success'
          });

          that.getFriendList();
        } else {
          that.setData({
            isShowModal: !that.data.isShowModal,
            rename: ''
          });
          $Toast({
            content: res.data.err,
            type: 'error'
          });
        }
      }
    });
  },

	getFriendInfo: function(e) {
		var that = this;
		var id = e.currentTarget.dataset.index;

		wx.showActionSheet({
			itemList: ['备注', '设为关心', '删除'],
			success: function (res) {
				if (res.tapIndex == 0) {
          that.setData({friend_id: id});
					that.showModal();
				} 

        if (res.tapIndex == 1) {
          that.setFocusFriend(id);
        } 
        
        if(res.tapIndex == 2) {
					that.deleteFriend(id);
				}
			},
			fail: function (res) {
				console.log(res.errMsg)
			}
		});
	},

  setFocusFriend: function(id) {
    var that = this;

    wx.request({
      url: util.basePath + '/article/v1/setFocusFriend',
      method: "post",
      data: {
        id: id
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data.status == 200) {
          $Toast({
            content: res.data.payload,
            type: 'success'
          });

          that.getFriendList();
        } else {
          $Toast({
            content: res.data.err,
            type: 'error'
          });
        }
      }
    });
  },

  deleteFriend: function(id) {
    var that = this;

    wx.request({
      url: util.basePath + '/article/v1/deleteFriend',
      method: "post",
      data: {
        id: id
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data.status == 200) {
          $Toast({
            content: res.data.payload,
            type: 'success'
          });

          that.getFriendList();
        } else {
          $Toast({
            content: res.data.err,
            type: 'error'
          });
        }
      }
    });
  },

  getFriendList: function() {
    var that = this;

    wx.request({
      url: util.basePath + '/article/v1/getFriendList',
      method: "post",
      data: {
        account: that.data.userInfo.account,
        app_sid: that.data.userInfo.app_sid
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        if (res.data.status == 200) {
					that.setData({
						friendList: res.data.payload,
						tip: '已经到底了'
					});
        } else {
					that.setData({
						tip: '暂无好友'
					});
				}
      }
    });
  },
	onSearch: function (event) {
		var friendphone = event.detail.value;
		var that = this;

		wx.request({
			url: util.basePath + '/article/v1/getFriendInfo',
			method: "post",
			data: {
				account: friendphone,
				app_sid: that.data.userInfo.app_sid
			},
			header: {
				'content-type': 'application/json'
			},
			success(res) {
				if (res.data.status == 200) {
					wx.setStorage({
						key: 'friendInfo',
						data: res.data.payload
					});

					wx.navigateTo({
						url: '/pages/friendinfo/friendinfo',
					});
				} else {
					$Toast({
						content: res.data.err,
						type: 'error'
					});
				}
			}
		});
	},
});